<?php
namespace Admin\Controller;
use Think\Controller;
class LoginController extends Controller {

	public function index(){
		
   		$this->redirect("login");
    }


    public function login(){
        if($_SESSION['user']['user_id']){

        	$this->redirect("Index/index");
        }
        
        	
        if (IS_POST) {
            $map = I("post.");  
            $verify_code = checkVerify($map['code']);
            if (!$verify_code) {
                $checkInfo = array('code' => 100, 'mess' => "验证码错误");
                echo json_encode($checkInfo);
                exit;
            }
            $userData = array(
                'username' => $map['username'],
                'password' => md5($map['password']),   
                'is_apply'=>"1"
            );
            $userInfo = M('admin')->where($userData)->find();
            if (!$userInfo) {
                $checkInfo = array('code' => 100, 'mess' => "账号或密码错误");
                echo json_encode($checkInfo);
                exit;
            }

            $ip = getUserIp();

            $data = array(
                'id' => $userInfo['id'],
                'last_ip' => $ip
            );
            M('admin')->save($data);

            session('admin', $userInfo);
            $checkInfo = array('code' => 200, 'mess' => "登录成功");
            echo json_encode($checkInfo);
            exit;
        } else {
            $this->display();
        }


    }

    //登出会员
    public function loginOut() {

        $_SESSION = ""; //清除SESSION值.
        if (isset($_COOKIE[session_name()])) {  //判断客户端的cookie文件是否存在,存在的话将其设置为过期.
            setcookie(session_name(), '', time() - 1, '/');
        }

        session_destroy();  //清除服务器的sesion文件
        $this->redirect("/Admin/Login/login");
    }
}